package com.example.testdbsearchform;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.Application;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	CategoryDAO datasource;
	CustomSQLiteHelper dbHelper;
	ArrayAdapter<Category> dataAdapter;
	
	Spinner  spinner;
	TextView date_view;
	EditText product_name;
	SimpleDateFormat sdf;
	
	static final int DATE_DIALOG_ID = 1;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        CustomApplication app = (CustomApplication)getApplication();
        sdf = new SimpleDateFormat("dd.MM.yy");
        
        dbHelper = CustomSQLiteHelper.getInstance(this);
        SQLiteDatabase database = dbHelper.open(); 
        datasource = new CategoryDAO(database);
        
        app.setCategories(datasource.getAllCategorys());
        List<Category> list = app.getCategories();
        
        Button sendButton = (Button)findViewById(R.id.submit);
        Button allButton = (Button)findViewById(R.id.all);
        Button dateButton = (Button)findViewById(R.id.product_date_submit);
        
        product_name = (EditText) findViewById(R.id.product_name);
        spinner = (Spinner) findViewById(R.id.product_category);
        date_view = (TextView) findViewById(R.id.product_date_view);
        
        Date date = new Date();
		date_view.setText(sdf.format(date));
        
		dataAdapter = new ArrayAdapter<Category>(this,
        		android.R.layout.simple_spinner_item, list);
    	dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinner.setAdapter(dataAdapter);
    	
    	spinner.setOnItemSelectedListener(chooseListener);
    	sendButton.setOnClickListener(sendListener);
    	allButton.setOnClickListener(allListener);
    	dateButton.setOnClickListener(dateListener);
    }
    
    @Override
    protected void onDestroy() {
    	dbHelper.close();
    	super.onDestroy();
    }
    
    @Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			Date date = new Date();
			final Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR); //date.getYear(); 
			int month = c.get(Calendar.MONTH); //date.getMonth();
			int day = c.get(Calendar.DAY_OF_MONTH); //date.getDay();
		   // set date picker as current date
		   return new DatePickerDialog(this, datePickerListener, year, month, day);
		}
		return null;
	}
    
    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
	public void onDateSet( DatePicker view, int year, int month, int day) {
	
		// set selected date into textview
		Date date = new Date(year, month, day);
		date_view.setText(sdf.format(date));
		}
	};
    
    private OnClickListener dateListener = new OnClickListener() {
		
		public void onClick(View v) {
			showDialog(DATE_DIALOG_ID);
	    }
	};
	
	private OnClickListener allListener = new OnClickListener() {
		
		public void onClick(View v) {			
			Intent intent = new Intent(MainActivity.this, ProdactListActivity.class);
			
			intent.putExtra("product_name", "");
			intent.putExtra("product_category", 0);
			intent.putExtra("product_date", "");
			
			startActivity(intent);
	    }
	};
    
    private OnClickListener sendListener = new OnClickListener() {
		
		public void onClick(View v) {			
			Intent intent = new Intent(MainActivity.this, ProdactListActivity.class);
			intent.putExtra("product_name", product_name.getText().toString());
			Category category = (Category) dataAdapter.getItem(spinner.getSelectedItemPosition());
			intent.putExtra("product_category", category.getId());
			intent.putExtra("product_date", date_view.getText());
			startActivity(intent);
	    }
	};
    
    private OnItemSelectedListener  chooseListener = new OnItemSelectedListener () {
		
    	public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    		Toast.makeText(parent.getContext(), 
    			"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
    			Toast.LENGTH_SHORT).show();
    	  }

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
    	 
	};
    
}
